package cuongnh.project.japanesediary.vocabulary.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name="tbl_sentence")
public class Sentence extends AbstractModelObject{

	@Id
	@Column(name = "sentence_id")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="sentence_seq")
	@SequenceGenerator(
			name="sentence_seq",
			sequenceName="sentence_sequence",
			allocationSize=1
		)
	private Long sentenceId;
	
	@Column(name="sentence", length=500)
	private String sentence;
	
	@Column(name="meaning_vietnamese", length=500)
	private String vnMeaning;
	
	@Column(name="meaning_english", length=500)
	private String enMeaning;
	
	@Column(name="meaning_japanese", length=500)
	private String jpMeaning;
	
	@ManyToMany(mappedBy="vocabularyExamples")    
	private Set<Vocabulary> vocabularies = new HashSet<Vocabulary>();
	
	@ManyToMany(mappedBy="grammarExamples")    
	private Set<Grammar> grammars = new HashSet<Grammar>();
	
	@ManyToMany(cascade={CascadeType.ALL}, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_sentence_category", joinColumns = { @JoinColumn(name = "sentence_id") }, inverseJoinColumns = { @JoinColumn(name = "category_id") })    
	private Set<Category> sentenceCategories = new HashSet<Category>();
	
	@Column(name = "created_date")
	private Date createdDate;

	@Column(name = "modified_date")
	private Date modifiedDate;

	public long getSentenceId() {
		return sentenceId;
	}

	public void setSentenceId(long sentenceId) {
		long oldValue = this.sentenceId;
		this.sentenceId = sentenceId;
		firePropertyChange("sentenceId", oldValue, sentenceId);
	}

	public String getSentence() {
		return sentence;
	}

	public void setSentence(String sentence) {
		String oldValue = this.sentence;
		this.sentence = sentence;
		firePropertyChange("sentence", oldValue, sentence);
	}

	public String getVnMeaning() {
		return vnMeaning;
	}

	public void setVnMeaning(String vnMeaning) {
		String oldValue = this.vnMeaning;
		this.vnMeaning = vnMeaning;
		firePropertyChange("vnMeaning", oldValue, vnMeaning);
	}

	public String getEnMeaning() {
		return enMeaning;
	}

	public void setEnMeaning(String enMeaning) {
		String oldValue = this.enMeaning;
		this.enMeaning = enMeaning;
		firePropertyChange("enMeaning", oldValue, enMeaning);
	}

	public String getJpMeaning() {
		return jpMeaning;
	}

	public void setJpMeaning(String jpMeaning) {
		String oldValue = this.jpMeaning;
		this.jpMeaning = jpMeaning;
		firePropertyChange("jpMeaning", oldValue, jpMeaning);
	}

	public Set<Vocabulary> getVocabularies() {		
		return vocabularies;
	}

	public void setVocabularies(Set<Vocabulary> vocabularies) {
		Set<Vocabulary> oldValue = this.vocabularies;
		this.vocabularies = vocabularies;
		firePropertyChange("vocabularies", oldValue, vocabularies);
	}

	public Set<Grammar> getGrammars() {
		return grammars;
	}

	public void setGrammars(Set<Grammar> grammars) {
		Set<Grammar> oldValue = this.grammars;
		this.grammars = grammars;
		firePropertyChange("grammars", oldValue, grammars);
	}

	public Set<Category> getSentenceCategories() {
		return sentenceCategories;
	}

	public void setSentenceCategories(Set<Category> sentenceCategories) {
		Set<Category> oldValue = this.sentenceCategories;
		this.sentenceCategories = sentenceCategories;
		firePropertyChange("sentenceCategories", oldValue, sentenceCategories);
	}
	
	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}
	
	public Date getModifiedDate() {
		return modifiedDate;
	}

	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}

	public void setSentenceId(Long sentenceId) {
		this.sentenceId = sentenceId;
	}

	@Override
	public boolean equals(Object obj) {
		boolean sameSame = false;

		if (obj != null && obj instanceof Sentence) {
			sameSame = sentenceId.equals(((Sentence) obj).sentenceId);
		}

		return sameSame;
	}
	
	@Override
	public int hashCode() {
		return this.sentenceId.intValue();
	}
	
}
